:- table a/2.

tw :- a(X,Y),write(X),write(' '),write(Y),nl,fail.
tw.

ts :- statistics,a(_X,_Y),fail.
ts :- statistics.

a(X,Y) :- p(X,Y).
a(X,Y) :- a(X,Z), a(Z,Y).

p(1,2).
p(2,3).
p(3,4).
p(4,5).
p(5,6).
p(6,7).
p(7,8).
p(8,9).
p(9,10).
p(10,11).
p(11,12).
p(12,13).
p(13,14).
p(14,15).
p(15,16).
p(16,17).
p(17,18).
p(18,19).
p(19,20).
p(20,21).
p(21,22).
p(22,23).
p(23,24).
p(24,25).
p(25,26).
p(26,27).
p(27,28).
p(28,29).
p(29,30).
p(30,31).
p(31,32).
p(32,33).
p(33,34).
p(34,1).

/* 
== Statistics ===
Permanent space: 120736 in use, 384K allocated.
Stack space allocated: 64K+64K+768K+768K+64K
Stacks in use : global 84, local 100, trail 156, cp 256
OLDT Stacks in use : table 0, tab_heap 0 opentables 0
num unwinds: 0
TIME: cputime: 0.40*1 sec, elapsetime 14.75 sec
== Statistics ===
Permanent space: 120736 in use, 384K allocated.
Stack space allocated: 64K+64K+768K+768K+64K
Stacks in use : global 92, local 88, trail 156, cp 220
OLDT Stacks in use : table 52080, tab_heap 0 opentables 0
num unwinds: 0
TIME: cputime: 4.67*1 sec, elapsetime 19.11 sec
*/
